package com.zlx.store.user.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.signers.JWTSigner;
import cn.hutool.jwt.signers.JWTSignerUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlx.store.common.constant.JWTSecurityKey;
import com.zlx.store.common.constant.LoginSend;
import com.zlx.store.common.constant.SecurityConstant;
import com.zlx.store.common.results.HttpResp;
import com.zlx.store.common.constant.RespCode;
import com.zlx.store.common.util.EmailTool;
import com.zlx.store.common.util.SMSUtils;
import com.zlx.store.common.util.StringTool;
import com.zlx.store.domain.entity.User;
import com.zlx.store.user.dao.StoreRelateUserDao;
import com.zlx.store.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequestMapping("api/code")
public class
VerifyCodeController {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    UserService userService;
    @Autowired
    EmailTool emailTool;
    @Autowired
    SMSUtils smsUtils;
    @Autowired
    StoreRelateUserDao storeRelateUserTabDao;
    @GetMapping("/getCode.jpg")
    public HttpResp creatCode(HttpServletRequest request) {
        LineCaptcha lineCaptcha=new LineCaptcha(200,100);//产生长方形验证码图片(前端可调整长宽)
        String code=lineCaptcha.getCode();//获取验证码code
        log.debug("生成的验证码是：{}",code);
        String image=lineCaptcha.getImageBase64();
        //获得IP地址
        String clientId=request.getRemoteAddr();
        //将ID和对应验证码code放进redis(key,value,生存时间,生存单位)
        redisTemplate.opsForValue().set("code-"+clientId,code,60, TimeUnit.SECONDS);
        return HttpResp.httpResp(RespCode.USER_OPERATION_SUCCESS,image,new Date());
    }
    @GetMapping("verifyByEP")
    public HttpResp logByEP(HttpServletRequest request, String input){
        if (StringTool.isEmailLegal(input)){
            User user=userService.selectUserByMail(input);
            if (Objects.nonNull(user)){
                String ecode=getEcode(request);
                String mailContent = String.format(LoginSend.MAIL_CONTENT.getMsg(),ecode);
                emailTool.sendMail(input, LoginSend.MAIL_SUBJECT.getMsg(), mailContent);
                return HttpResp.httpResp(RespCode.USER_OPERATION_SUCCESS,"验证码已发送",new Date());
            }
            return HttpResp.httpResp(RespCode.USER_LOGIN_ERROR_USERNAME_NO_FIND,"邮箱不存在",new Date());
        }
        if (StringTool.isChinaPhoneLegal(input)){
            User user=userService.selectUserByPhone(input);
            if (Objects.nonNull(user)){
                String ecode=getEcode(request);
                smsUtils.sendToPhone(input,ecode);
                return HttpResp.httpResp(RespCode.USER_OPERATION_SUCCESS,"验证码已发送",new Date());
            }
            return HttpResp.httpResp(RespCode.USER_LOGIN_ERROR_USERNAME_NO_FIND,"手机号不存在",new Date());
        }
        return HttpResp.httpResp(RespCode.USER_LOGIN_ERROR_USERNAME_NO_FIND,"手机或邮箱格式不正确",new Date());
    }
    private String getEcode(HttpServletRequest request){
        String ecode = RandomUtil.randomNumbers(6);
        String clientId=request.getRemoteAddr();
        redisTemplate.opsForValue().set("ecode-"+clientId,ecode,30, TimeUnit.MINUTES);
        return ecode;
    }

    @GetMapping("/verify")
    public HttpResp verify(HttpServletRequest request,String inputCode){//从客户端获取唯一ID值
        String clientId=request.getRemoteAddr();
        String trueCode=(String) redisTemplate.opsForValue().get("code-"+clientId);
        if (Objects.isNull(trueCode)){
            return HttpResp.httpResp(RespCode.VERIFY_CODE_ISNULL_ERROR,RespCode.VERIFY_CODE_ISNULL_ERROR.getMsg(),new Date());
        }
        else{
            if (trueCode.equals(inputCode)){
                return HttpResp.httpResp(RespCode.VERIFY_CODE_SUCCESS,RespCode.VERIFY_CODE_SUCCESS.getMsg(),new Date());
            }
            else {
                return HttpResp.httpResp(RespCode.VERIFY_CODE_ERROR,RespCode.VERIFY_CODE_ERROR.getMsg(),new Date());
            }
        }
    }
    @GetMapping("/verifyEcode")
    public HttpResp verifyEcode(HttpServletRequest request,String inputCode){//从客户端获取唯一ID值
        String clientId=request.getRemoteAddr();
        String trueCode=(String) redisTemplate.opsForValue().get("ecode-"+clientId);
        if (Objects.isNull(trueCode)){
            return HttpResp.httpResp(RespCode.VERIFY_CODE_ISNULL_ERROR,RespCode.VERIFY_CODE_ISNULL_ERROR.getMsg(),new Date());
        }
        else{
            if (trueCode.equals(inputCode)){

                return HttpResp.httpResp(RespCode.VERIFY_CODE_SUCCESS,RespCode.VERIFY_CODE_SUCCESS.getMsg(),new Date());
            }
            else {
                return HttpResp.httpResp(RespCode.VERIFY_CODE_ERROR,RespCode.VERIFY_CODE_ERROR.getMsg(),new Date());
            }
        }
    }
}
